package com.google.fbyoutube.shared;

import java.util.HashMap;
import java.util.Map;

public class FeedUrlMaker {

	public static String buildUrl(String sortBy, String cat, String region, Map<String, String> options) {
		String feedUrl = VIDEOS_FEED;
		if (MOST_POPULAR_FEED.equals(sortBy)){
			feedUrl = MOST_POPULAR_FEED;
			if (region != null && FeedUrlMaker.regionSpec.containsKey(region)) {
				feedUrl = STANDARD_FEED_PREFIX + FeedUrlMaker.regionSpec.get(region);
				feedUrl += "/most_popular";
			}
		} else if (TOP_RATED_FEED.equals(sortBy)) {
			feedUrl = TOP_RATED_FEED;
			if (region != null && FeedUrlMaker.regionSpec.containsKey(region)) {
				feedUrl = STANDARD_FEED_PREFIX + FeedUrlMaker.regionSpec.get(region)
						+ "/top_rated";
			}
		} else if (MOST_VIEWED_FEED.equals(sortBy)) {
			feedUrl = MOST_VIEWED_FEED;
			if (region != null && FeedUrlMaker.regionSpec.containsKey(region)) {
				feedUrl = STANDARD_FEED_PREFIX + FeedUrlMaker.regionSpec.get(region)
						+ "/most_viewed";
			}
		} else if (TREND_FEED.equals(sortBy)) {
			feedUrl = TREND_FEED;
			if (region != null && FeedUrlMaker.regionSpec.containsKey(region)) {
				feedUrl = STANDARD_FEED_PREFIX + FeedUrlMaker.regionSpec.get(region)
						+ "/on_the_web";
			}
		}
		
		feedUrl = appendUrlCategory(cat, feedUrl);

		// url parameters including time, start-index, max-results
		if (options == null) {
			options = new HashMap<String, String>();
		}
				
		if (!options.containsKey(START_INDEX)) {
			options.put(START_INDEX, DEFAULT_START_INDEX);
		}
		if (!options.containsKey(MAX_RESULTS)) {
			// This parameter is essential
			options.put(MAX_RESULTS, DEFAULT_MAX_RESULTS);
		}
		feedUrl = appendUrlOptions(feedUrl, options);
		return feedUrl;
	}

	private static String appendUrlCategory(String cat, String feedUrl) {
		if (cat != null && !cat.isEmpty()) {
			feedUrl += "_" + cat;
		}
		return feedUrl;
	}

	private static String appendUrlOptions(String feedUrl, Map<String, String> options) {
		feedUrl = setFeedVersion(feedUrl, 2);
		if (options != null) {
			for (String key : options.keySet()) {
				feedUrl += "&" + key + "=" + options.get(key);
			}
		}
		return feedUrl;
	}
	
	private static String setFeedVersion(String feedUrl, int feedVersion) {
		return feedUrl + "?v=" + feedVersion;
	}
	
	public static final Map<String, String> regionSpec = new HashMap<String, String>();
	public static final String wwAll = "Worldwide (ALL)";
	
	static {
		regionSpec.put("Australia", "AU");
		regionSpec.put("Brazil", "BR");
		regionSpec.put("Canada", "CA");
		regionSpec.put("Czech Republic", "CZ");
		regionSpec.put("France", "FR");
		regionSpec.put("Germany", "DE");
		regionSpec.put("Great Britain", "GB");
		regionSpec.put("Holland", "NL");
		regionSpec.put("Hong Kong", "HK");
		regionSpec.put("India", "IN");
		regionSpec.put("Ireland", "IE");
		regionSpec.put("Israel", "IL");
		regionSpec.put("Italy", "IT");
		regionSpec.put("Japan", "JP");
		regionSpec.put("Mexico", "MX");
		regionSpec.put("New Zealand", "NZ");
		regionSpec.put("Poland", "PL");
		regionSpec.put("Russia", "RU");
		regionSpec.put("South Korea", "KR");
		regionSpec.put("Spain", "ES");
		regionSpec.put("Sweden", "SE");
		regionSpec.put("Taiwan", "TW");
		//
		regionSpec.put("Worldwide (ALL)", "US");
	}
	
	public static final Map<String, String> categorySpec = new HashMap<String, String>();
	
	static {
		categorySpec.put("Film", "Film & Animation");
		categorySpec.put("Autos", "Auto & Vehicles");
		categorySpec.put("Music", "Music");
		categorySpec.put("Animals", "Pets a& Animals");
		categorySpec.put("Sports", "Sports");
		categorySpec.put("Travel", "Travel & Events");
		categorySpec.put("Games", "Gaming");
		categorySpec.put("Comedy", "Comedy");
		categorySpec.put("People", "People & Blogs");
		categorySpec.put("News", "News & Politics");
		categorySpec.put("Entertainment", "Entertainment");
		categorySpec.put("Education", "Education");
		categorySpec.put("Howto", "How & Style");
		categorySpec.put("Tech", "Science & Technology");
		categorySpec.put(null, "All Categories");
	}
	
	public static final Map<String, String> timeSpec = new HashMap<String, String>();
	public static final String DefaultDueTime = "today";
	
	static {
		timeSpec.put("Today", "today");
		timeSpec.put("This week", "this_week");
		timeSpec.put("This month", "this_month");
	}
	
	public static final Map<String, String> typeSpec = new HashMap<String, String>();
	
	/**
	 * The name of the server hosting the YouTube GDATA feeds
	 */
	public static final String YOUTUBE_GDATA_SERVER = "https://gdata.youtube.com";

	/**
	 * The prefix common to all standard feeds
	 */
	public static final String STANDARD_FEED_PREFIX = YOUTUBE_GDATA_SERVER
			+ "/feeds/api/standardfeeds/";

	/**
	 * The URL of the "Most Recent" feed
	 */
	public static final String MOST_RECENT_FEED = STANDARD_FEED_PREFIX
			+ "most_recent";

	/**
	 * The URL of the "Most Recent" feed
	 */
	public static final String MOST_POPULAR_FEED = STANDARD_FEED_PREFIX
			+ "most_popular";

	/**
	 * The URL of the "Top Favorites" feed
	 */
	public static final String TOP_FAVORITE_FEED = STANDARD_FEED_PREFIX
			+ "top_favorites";

	/**
	 * The URL of the "Top Rated" feed
	 */
	public static final String TOP_RATED_FEED = STANDARD_FEED_PREFIX
			+ "top_rated";

	/**
	 * The URL of the "Most Viewed" feed
	 */
	public static final String MOST_VIEWED_FEED = STANDARD_FEED_PREFIX
			+ "most_viewed";
	
	/**
	 * The URL of the "Most Shared" feed
	 */
	public static final String MOST_SHARED_FEED = STANDARD_FEED_PREFIX
			+ "most_shared";
	
	/**
	 * The URL of the "Most discussed" feed
	 */
	public static final String MOST_DISCUSSED_FEED = STANDARD_FEED_PREFIX
			+ "most_discussed";
	
	/**
	 * The URL of the "Most responsed" feed
	 */
	public static final String MOST_RESPONSED_FEED = STANDARD_FEED_PREFIX
			+ "most_responded";
	
	/**
	 * The URL of the "YouTube trend" feed
	 */
	public static final String TREND_FEED = STANDARD_FEED_PREFIX
			+ "on_the_web";

	/**
	 * The URL of the "Videos" feed
	 */
	public static final String VIDEOS_FEED = YOUTUBE_GDATA_SERVER
			+ "/feeds/api/videos";
	
	/**
	 * The prefix of the User Feeds
	 */
	public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER
			+ "/feeds/api/users/";

	/**
	 * The URL suffix of the test user's uploads feed
	 */
	public static final String UPLOADS_FEED_SUFFIX = "/uploads";

	/**
	 * The URL suffix of the test user's favorites feed
	 */
	public static final String FAVORITES_FEED_SUFFIX = "/favorites";

	/**
	 * The URL suffix of the test user's recommendations feed
	 */
	public static final String RECOMMENDATIONS_FEED_SUFFIX = "/recommendations";

	/**
	 * The URL suffix of the test user's subscriptions feed
	 */
	public static final String SUBSCRIPTIONS_FEED_SUFFIX = "/subscriptions";

	/**
	 * The URL suffix of the test user's playlists feed
	 */
	public static final String PLAYLISTS_FEED_SUFFIX = "/playlists";
	
	public static final String START_INDEX = "start-index";
	
	public static final String MAX_RESULTS = "max-results";
	
	public static final String TIME = "time";
	
	public static final String DEFAULT_CAT = null;
	
	public static final String DEFAULT_START_INDEX = "1";
	
	public static final String DEFAULT_MAX_RESULTS = "25";
	
	static {
		typeSpec.put("Most Popular", MOST_POPULAR_FEED);
		typeSpec.put("Most Viewed", MOST_VIEWED_FEED);
		typeSpec.put("Top Rated", TOP_RATED_FEED);
	}
}
